package Interface.Producao.Descontos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import sft.BancodeDados.CriaConexao;
import sft.Logica.Descontos;

public class DescontosDao {
    
    private static Connection conexao;
    
    public DescontosDao() throws SQLException {
        conexao = new CriaConexao().getConnection();
    }    
    //Data;Codigo OS;Paciente;Total;Desconto;Liquido;Autorizado por;Atendente
    public void adicionar(List<Descontos> d) throws SQLException {
        try (PreparedStatement stmt = conexao.prepareStatement("INSERT INTO sft.sft_descontos ("
                + "desc_dt_data, desc_os_os, desc_nm_nome, "
                + "desc_vl_total, desc_vl_desconto, desc_vl_liquido, "
                + "desc_nm_autorizado, desc_nm_atendente) " +
"	VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) {
            
            for (int i = 0; i < d.size(); i++) {
                
                stmt.setString(1, d.get(i).getData());
                stmt.setString(2, d.get(i).getOs());
                stmt.setString(3, d.get(i).getNome());
                stmt.setDouble(4, d.get(i).getTotal());
                stmt.setDouble(5, d.get(i).getDesconto());
                stmt.setDouble(6, d.get(i).getLiquido());
                stmt.setString(7, d.get(i).getAutorizado());
                stmt.setString(8, d.get(i).getAtendente());
                stmt.execute();
                stmt.clearParameters();
            }
            
            stmt.close();
        }
    }
    
    public void clearTabela() throws SQLException{
        try (PreparedStatement stmt = conexao.prepareStatement("TRUNCATE TABLE `sft`.`sft_descontos`")) {
            
            stmt.execute();
        }
    }

    List<Descontos> getDescontos() throws SQLException {
        PreparedStatement stmt = conexao.prepareStatement("select * from sft_descontos");
        ResultSet rs = stmt.executeQuery();
        
        List<Descontos> list = new ArrayList<>();
        
        while(rs.next()){
            Descontos d = new Descontos();
            d.setData(rs.getString(2));
            d.setOs(rs.getString(3));
            d.setNome(rs.getString(4));
            d.setTotal(rs.getDouble(5));
            d.setDesconto(rs.getDouble(6));
            d.setLiquido(rs.getDouble(7));
            d.setAtutorizado(rs.getString(8));
            d.setAtendente(rs.getString(9));
            list.add(d);
        }
        
        return list;
    }

    public List<Descontos> getDescontos(String text) throws SQLException {
        PreparedStatement stmt = conexao.prepareStatement("select * from sft_descontos where desc_os_os like ?");
        stmt.setString(1, "%" + text + "%");
        ResultSet rs = stmt.executeQuery();
        
        List<Descontos> list = new ArrayList<>();
        
        while(rs.next()){
            Descontos d = new Descontos();
            d.setData(rs.getString(2));
            d.setOs(rs.getString(3));
            d.setNome(rs.getString(4));
            d.setTotal(rs.getDouble(5));
            d.setDesconto(rs.getDouble(6));
            d.setLiquido(rs.getDouble(7));
            d.setAtutorizado(rs.getString(8));
            d.setAtendente(rs.getString(9));
            list.add(d);
        }
        
        return list;
    }
    
    
    
}
